<template>
  <div class="app-container">
    <avue-crud
      :option="option"
      :data="data"
      :table-loading="loading"
      :page.sync="page"
      @on-load="onLoad"
      @current-change="currentChange"
      @search-change="searchChange"
      @search-reset="resetChange"
    >
      <template
        slot="icon"
        slot-scope="scope"
      >
        <div v-if="scope.row.icon">
          <svg-icon :icon-class="scope.row.icon"></svg-icon>
        </div>
      </template>

      <template
        slot-scope="{type,size,row}"
        slot="menu"
      >
        <el-button
          icon="el-icon-check"
          :size="size"
          v-show="row.status==='3000'"
          :type="type"
          @click="toSubmit(row)"
        >提 交</el-button>
        <el-button
          icon="el-icon-edit"
          :size="size"
          v-show="row.status==='3000'"
          :type="type"
          @click="toEdit(row)"
        >编 辑</el-button>
        <el-button
          icon="el-icon-close"
          :size="size"
          v-show="row.status==='3000'"
          :type="type"
          @click="rowDel(row)"
        >删 除</el-button>
        <el-button
          icon="el-icon-view"
          :size="size"
          v-show="row.status!=='3000'"
          :type="type"
          @click="toDet(row,false)"
        >查 看</el-button>

      </template>
    </avue-crud>
    <el-dialog
      title="融资申请"
      class="avue-dialog"
      top="5vh"
      :visible.sync="box"
      width="55%"
      @opened="open"
    >
      <el-form
        :model="form"
        :disabled="false"
        :rules="rules"
        ref="form"
        label-width="130px"
        class="form"
      >
        <el-row>
          <el-col :span="24">
            <el-divider content-position="left"><i class="el-icon-info"></i> 企业信息</el-divider>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业名称">
              <el-input v-model="form.qymc"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社会信用代码">
              <el-input v-model="form.qyshxydm"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册时间">
              <el-input v-model="form.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册资金">
              <el-input v-model="form.zczb"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属行业">
              <el-input v-model="form.sshy"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属区域">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业类型">
              <el-input v-model="form.qylx"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业联系方式">
              <el-input v-model="form.qylxfs"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册地址">
              <el-input v-model="form.zcdz"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left"><i class="el-icon-info"></i> 法人信息</el-divider>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名">
              <el-input v-model="form.frxm"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-input v-model="form.sex"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期">
              <el-input v-model="form.birthday"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="籍贯">
              <el-input v-model="form.nativePlace"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚姻状况">
              <el-input v-model="form.maritalStatus"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历">
              <el-input v-model="form.education"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号">
              <el-input v-model="form.idCard"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input v-model="form.email"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系地址">
              <el-input v-model="form.contactAdress"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left"><i class="el-icon-info"></i> 需求信息</el-divider>
          </el-col>
          <el-col :span="12">
            <el-form-item label="需求金额">
              <el-input v-model="form.demandAmount"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="需求金额大写">
              <el-input v-model="form.demandAmountCapitalized"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="担保方式">
              <el-input v-model="form.guarantyType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="融资期限(月)">
              <el-input v-model="form.rzqx"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="融资用途">
              <el-input v-model="form.financingPurpose"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="还款来源">
              <el-input v-model="form.repaySource"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left"><i class="el-icon-info"></i> 经办人信息</el-divider>
          </el-col>
          <el-col :span="24">
            <el-table
              border
              :data="form.tabData"
            >
              <el-table-column
                type="index"
                label="序号"
                width="100"
                align="center"
              >
              </el-table-column>
              <el-table-column
                prop="cuser"
                label="姓名"
              >
              </el-table-column>
              <el-table-column
                prop="eid"
                label="工号"
              >
              </el-table-column>
              <el-table-column
                prop="phone"
                label="联系方式"
              >
              </el-table-column>
              <el-table-column
                prop="department"
                label="所属部门"
              >
              </el-table-column>
              <el-table-column
                prop="position"
                label="职位"
              >
              </el-table-column>
              <el-table-column
                prop="operation"
                label="操作"
              >
              </el-table-column>
              <el-table-column
                :show-overflow-tooltip="true"
                prop="createTime"
                label="操作时间"
              >
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left"><i class="el-icon-info"></i>预留收款账户信息</el-divider>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款开户银行">
              <el-input v-model="form.receiveBank"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款开户行">
              <el-input v-model="form.receiveAccountBank"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款人">
              <el-input v-model="form.receivePayee"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款账号">
              <el-input v-model="form.receiveAccount"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          plain
          @click="resetForm('form')"
        >取 消</el-button>
        <el-button
          v-show="form.noBtn"
          type="primary"
          @click="rowUpdate('zc')"
        >暂 存</el-button>
        <el-button
          v-show="form.noBtn"
          type="primary"
          @click="rowUpdate('fb')"
        >发 布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addOrder,
  pubOrder,
  delOrder,
  getOrder,
  orderDet,
} from '@/api/manager.js'
export default {
  data() {
    return {
      /* 弹窗参数 */
      box: false,
      form: {},
      loading: false,
      rules: {},
      /* 分页 */
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      data: [
        {
          id: 1,
          deptValue: '机构1',
          orgCode: '编号',
          deptPid: '上级机构',
          oindex: 1,
          icon: 'back',
          note: '信息描述',
        },
      ],
      option: {
        headerAlign: 'center',
        align: 'center',
        border: true,
        index: true,
        indexLabel: '序号',
        indexWidth: 100,
        card: true,
        defaultExpandAll: true,
        dialogDrag: false,
        labelWidth: 135,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        searchSpan: 8,
        searchMenuSpan: 4,
        searchLabelWidth: '20%',
        column: [
          {
            label: '融资编号',
            prop: 'orderCode',
            search: true,
          },
          {
            label: '企业名称',
            prop: 'companyName',
            display: false,
            search: true,
          },
          {
            label: '社会统一信用代码',
            prop: 'companyCode',
            display: false,
            search: true,
          },
          {
            label: '企业类型',
            prop: 'companyType',
            type: 'select',
            search: true,
            dicData: [
              {
                label: '小微企业',
                value: '1',
              },
              {
                label: '中型企业',
                value: '2',
              },
              {
                label: '大型企业',
                value: '3',
              },
              {
                label: '超大型企业',
                value: '4',
              },
              {
                label: '个体工商户',
                value: '5',
              },
            ],
            display: false,
          },
          {
            label: '法人姓名',
            prop: 'name',
            display: false,
            search: true,
          },
          {
            label: '申请额度(万元)',
            prop: 'demandAmount',
            display: false,
          },
          {
            label: '融资状态',
            prop: 'status',
            search: true,
            dicData: [
              {
                label: '待提交',
                value: '3000',
              },
              {
                label: '授信审批中',
                value: '4000',
              },
              {
                label: '授信失败',
                value: '4001',
              },
              {
                label: '放款审批中',
                value: '5000',
              },
              {
                label: '放款失败',
                value: '5001',
              },
              {
                label: '待放款',
                value: '5002',
              },
              {
                label: '还款中',
                value: '6000',
              },
              {
                label: '还款中',
                value: '7000',
              },
            ],
            display: false,
          },
          {
            label: '申请时间',
            prop: 'loanDate',
            display: false,
          },
        ],
      },
    }
  },
  methods: {
    /* 获取订单列表 */
    onLoad(page, params = {}) {
      if (page.layout) {
        Object.assign(params, {
          page: 1,
          size: 10,
        })
      } else {
        Object.assign(params, page)
      }
      this.loading = true
      getOrder(params).then((data) => {
        if (data.status) {
          this.loading = false
          this.data = data.data.records
          this.page.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    /* 清空 */
    resetChange() {
      this.onLoad({
        currentPage: 1,
        pageSize: 10,
      })
    },
    /* 搜索筛选 */
    searchChange(params, done) {
      done()
      params.currentPage = 1
      params.pageSize = 10
      this.onLoad(params)
    },
    toEdit(row) {
      this.box = true
      /* 赋值code */
      this.form.orderCode = row.orderCode
    },
    /* 取消 */
    resetForm() {
      this.box = false
    },
    /* 提交 */
    toSubmit(row) {
      let _this = this
      this.open(row.orderCode)
      this.$confirm('确定将选择数据提交?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let form = _this.form
        pubOrder(form).then((data) => {
          if (data.status) {
            _this.box = false
            _this.onLoad(this.page)
            _this.$message.success('操作成功!')
          } else {
            _this.$message.error(data.msg)
          }
        })
      })
    },
    /* 修改 */
    rowUpdate(t) {
      if (t == 'zc') {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let form = this.form
            addOrder(form).then((data) => {
              if (data.status) {
                this.box = false
                this.onLoad(this.page)
                this.$message.success('暂存成功!')
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let form = this.form
            pubOrder(form).then((data) => {
              if (data.status) {
                this.box = false
                this.onLoad(this.page)
                this.$message.success('发布成功!')
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    },
    /* 改变页码 */
    currentChange(currentPage) {
      this.page.currentPage = currentPage
      this.onLoad(this.page)
    },
    /* 删除 */
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delOrder({
          orderCode: row.orderCode,
        }).then((data) => {
          if (data.status) {
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    /* 查看详情 */
    toDet(row, flag) {
      this.box = true
      /* 赋值code */
      this.form.orderCode = row.orderCode
      this.form.noBtn = flag
    },
    /* 开启弹窗时赋值详情数据 */
    open(e) {
      orderDet({
        orderCode: e || this.form.orderCode,
      }).then((data) => {
        if (data.status) {
          let { jrfkCustomerBaseDto, jrfkOrderBase, jrfkOrderRecordList } =
            data.data
          let form = {
            tabData: jrfkOrderRecordList,
            ...jrfkCustomerBaseDto,
            ...jrfkOrderBase,
          }
          this.form = form
        } else {
          this.$message.error(data.msg)
        }
      })
    },
  },
}
</script>
